package com.oracle.lingshangejia.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.bean.ExpertVideoTitleBean;

import java.util.List;

public class ExpertVideoTitleAdapter extends RecyclerView.Adapter<ExpertVideoTitleAdapter.ViewHolder> {

    private Context context;
    private int index = 0;
    private List<ExpertVideoTitleBean.DataBean> data;
    private OnItemClickListener listener;
    public interface OnItemClickListener {
        void onItemClick(View view, int whyId);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    public ExpertVideoTitleAdapter(Context context, List<ExpertVideoTitleBean.DataBean> data){
        this.context = context;
        this.data=data;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_expert_video_title,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

        if (i == index){
            viewHolder.tv_title.setTextColor(Color.parseColor("#0078FF"));
            viewHolder.tv_line.setVisibility(View.VISIBLE);
        }else {
            viewHolder.tv_title.setTextColor(Color.parseColor("#000000"));
            viewHolder.tv_line.setVisibility(View.INVISIBLE);
        }
        viewHolder.tv_title.setText(data.get(i).getName());
        viewHolder.itemView.setOnClickListener((v -> {
            if (listener != null) {
                listener.onItemClick(v,i);
            }
            index = i;
            notifyDataSetChanged();
        }));
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        TextView tv_title,tv_line;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tv_title = itemView.findViewById(R.id.tv_title);
            tv_line = itemView.findViewById(R.id.tv_line);
        }
    }
}
